package inet.db.tennis.buffer;

import java.util.*;
import java.math.BigDecimal;
import inet.db.tennis.TennisCategory;
import inet.db.tennis.TennisCategoryDAO;
import inet.db.tennis.TennisChampion;
import inet.db.tennis.TennisChampionDAO;

public class TennisChampionBuffer extends Thread {
    private static String object = "";

    private static Map championTable = new Hashtable(); //code - FootballChampion
    private static Map championIdTable = new Hashtable(); //id - FootballChampion
    private static Vector championList = new Vector();

    // Category Buffer
    private static Map categoriesIdTable = new Hashtable();
    private static Vector categoriesList = new Vector();
    
    public static Map getChampionTable() {
        synchronized(object) {
            return championTable;
        }
    }

    public static TennisChampion getChampion(String code) {
        synchronized(object) {
            return (TennisChampion) championTable.get(code);
        }
    }

    public static TennisChampion getChampion(BigDecimal id) {
        synchronized(object) {
            return (TennisChampion) championIdTable.get(id);
        }
    }

    public static Vector getChampionList() {
        synchronized(object) {
            return championList;
        }
    }

    public static TennisCategory getCategory(BigDecimal id) {
        synchronized(object) {
            return (TennisCategory) categoriesIdTable.get(id);
        }
    }
    
    public static Vector getCategoriesList() {
        synchronized(object) {
            return categoriesList;
        }
    }
    
    static {
        new TennisChampionBuffer().start();
    }

    public void run() {
        while(true) {
            try {
                load();
                loadCategories();
                sleep(20 * 60 * 1000); //20p
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    private void load() {
        System.out.println("~Loading Tennis championBuffer...");
        TennisChampionDAO tennisChampionDAO = new TennisChampionDAO();
        Vector<TennisChampion> vChampion = tennisChampionDAO.findChampionRunning(); //find all champion <> STOP

        Vector v = new Vector();
        Map m = new Hashtable();
        Map m2 = new Hashtable();

        TennisChampion champion = null;
        if(vChampion != null ){
	        for (Iterator it = vChampion.iterator(); it.hasNext(); ) {
	            champion = (TennisChampion) it.next();
	            if (champion == null) continue;
	            v.add(champion);
	            m.put(champion.getCode(), champion);
	            m2.put(champion.getId(), champion);
	        }
        }

        //Sắp xếp theo tên
        Collections.sort(v);

        synchronized(object) {
            championTable.clear();
            championTable.putAll(m);

            championIdTable.clear();
            championIdTable.putAll(m2);

            championList.clear();
            championList.addAll(v);
        }
        System.out.println("~Load Tennis championBuffer OK!");
    }
    
    private void loadCategories() {
    	System.out.println("~Loading Tennis categoriesBuffer...");
    	TennisCategoryDAO tennisCateDAO = new TennisCategoryDAO();
    	Vector<TennisCategory> vCategories = tennisCateDAO.findAll(); //find all champion <> STOP
    	
    	Vector v = new Vector();
    	Map m = new Hashtable();
    	
    	TennisCategory cate = null;
    	if(vCategories != null ){
    		for (Iterator it = vCategories.iterator(); it.hasNext(); ) {
    			cate = (TennisCategory) it.next();
    			if (cate == null) continue;
    			v.add(cate);
    			m.put(cate.getId(), cate);
    		}
    	}
    	
    	//Sắp xếp theo tên
    	Collections.sort(v);
    	
    	synchronized(object) {
    		categoriesIdTable.clear();
    		categoriesIdTable.putAll(m);
    		
    		categoriesList.clear();
    		categoriesList.addAll(v);
    	}
    	System.out.println("~Load Tennis categoriesBuffer OK!");
    }
}
